/**Created by the LayaAirIDE*/
module view.dlg{
	var AutomaticAcquisition = 12;
	var SetPhysicaTimer = 7200;
	export class LuckDarw extends ui.dlg.LuckDarwUI{
			private rewardShifting:Array<number>;
		    private the_time: boolean;
			private scaleDelta: number = 0;
			private cntNum :number = 30;
			private curDegrees:number;// 需要转到哪一度，比如第一格是0 360/16度，第二格是360/8 +360/16
			private darwId:number ; //显示的种子id
			private darwLevel:number;
			private lastDifference:number;
			private lastTime:boolean;
			private borderDifference = 17; //边界度数上下5度
			private prize:Prize;
			private darwState:boolean;
			private physical:number;
			private textTime:number;
			private prizeMultiple:number;//奖励倍数
			
		constructor(physical:number,textTime:number){
			super();
			this.rewardShifting =[6,1,5,2,8,7,3,4];
			this.the_time = false;
			this.lastTime = false;
			this.zhezhao.visible = false;
			this.darwState = true;
			this.physical = physical;
			this.textTime = textTime;
			this.upPhysical();

			this.anjian.on(Laya.Event.CLICK,this,this.start);
			this.anjian.on(Laya.Event.MOUSE_OUT,this,this.anjianOut);
			this.anjian.on(Laya.Event.MOUSE_OVER,this,this.anjianShow);
			
			this.anjian_ani.play();
			this.anjian_dong.visible = true;
			this.zhizhen.on(Laya.Event.CLICK,this,this.zhuanpanFun);
			Laya.timer.loop(2000,this,this.setDarwState);
		}
		anjianShow():void{
			this.anjian_ani.stop();
			this.anjian_dong.visible = false;
		}
		anjianOut():void{
			this.anjian_ani.play();
			this.anjian_dong.visible = true;
		}
		start():void {
			
			if (this.physical < 1){
				return;
			}

			if(this.darwState){
				CtrlManager.GetCtrl(CtrlNames.Darw).sendLuckDarw();
				this.darwState = false;
			}
		}
		zhuanpanFun():void{

		}
		createApe(darw_id:number,darw_level:number, physical:number,textTime:number,prizeMultiple:number): void {

			this.physical = physical;
			this.textTime = textTime;
			this.upPhysical();
			this.darwLevel = darw_level;
			this.prizeMultiple = prizeMultiple;
			this.curDegrees = this.rewardShifting[darw_level - 1];
			
			this.darwId = darw_id;
			this.zhezhao.visible = true;
			Laya.timer.frameLoop(1, this, this.animate);
			Laya.timer.once(400,this,this.stop);
    	}

		private animate(e: Event): void {
			//转动速度
			if (this.the_time) {
				
				this.the_time = false;
				var cur_rotation = this.zhizhen.rotation % 360;
				Laya.timer.clear(this,this.animate);

				this.lastTime = true;
				Laya.timer.frameLoop(1, this, this.animate);

			}
			else {
				
				if (this.cntNum > 5){           
					this.cntNum -= 0.6
				}
				if (this.cntNum < 10) {
					this.zhezhao.visible = false;
				}
				if (this.lastTime && this.cntNum < 6){				
					var cur_rotation = this.zhizhen.rotation % 360;
					if ( (this.curDegrees != 1 && (360/16+(this.curDegrees -1)*360/8 - this.borderDifference > cur_rotation &&
					360/16+(this.curDegrees -2 )*360/8 + this.borderDifference< cur_rotation ) )
					//360/16 + (this.curDegrees -2 )*360/8 - this.borderDifference < cur_rotation ) )
					|| (this.curDegrees == 1 && (360 - 360/16 + this.borderDifference < cur_rotation || 360/16 - this.borderDifference > cur_rotation)) ){
						Laya.timer.clear(this,this.animate);
						this.prize = new Prize();
						this.prize.popup();
						this.prize.darwShow(this.darwId,this.darwLevel);
						this.reset();
						return;
					}
					this.zhizhen.rotation += this.cntNum ;
				}
				else{
					this.zhizhen.rotation += this.cntNum ;
				}
				
			}
			//心跳缩放
			this.scaleDelta += 0.02;
			var scaleValue: number = Math.sin(this.scaleDelta);
			this.zhizhen.scale(1, 1);
			
		}
		setDarwState():void{
			this.darwState = true;
		}
		stop():void {
			this.the_time = true;
			Laya.Log.print("true");
		}
		reset():void {
			this.the_time = false;
			this.lastTime = false;
			this.cntNum = 30;
		}
		upPhysical():void {
			//对从服务器获取体力值和下次领取体力的时间显示
			if (this.physical <= AutomaticAcquisition){
				let data:any = {};
				let temp:number = this.physical;
				for (let i:number = 1;i>=0;i--){
					data["item"+i] = {index:Math.floor(temp%10)};
					temp /= 10;
				}
				this.timePhysical.dataSource = data;
				this.addPhysical.visible = false;
			}
			else{
				let data:any = {};
				
				data["item"+1] = {index:2};
				data["item"+0] = {index:1};
				this.timePhysical.dataSource = data;
				data = {}
				let temp:number = this.physical - AutomaticAcquisition;

				if (temp < 10){
					for (let i:number = 2;i>=0;i--){
						let item = this.addPhysical.getChildByName("item" + i) as Laya.Clip;
						if  (i == 0){
							data["item"+i] = {index:Math.floor(temp%10)};
							temp /= 10;
							item.visible = true;
						}else{
							item.visible = false;
						}				
					}
				}else if (temp <100){
					for (let i:number = 2;i>=0;i--){
						let item = this.addPhysical.getChildByName("item" + i) as Laya.Clip;
						if  (i == 2){
							item.visible = false;
						}else{				
							data["item"+i] = {index:Math.floor(temp%10)};
							temp /= 10;
							item.visible = true;
						}				
					}
				}else if(temp <1000){
					for (let i:number = 2;i>=0;i--){
						data["item"+i] = {index:Math.floor(temp%10)};
						temp /= 10;
						this.addPhysical.visible = true;
					}
				}
				this.addPhysical.dataSource = data;
				this.addPhysical.dataSource = data;
				this.addPhysical.visible = true;
			}
		}

		showSurplusTime():number{
			if(this.textTime == 0 || this.physical >= AutomaticAcquisition) {
				this.showTime.visible = false;
				return 1;
			}
			this.textTime -=  1;
			if(this.textTime == 0 ){
				this.physical += 1;
				this.upPhysical();
				if ( this.physical >= AutomaticAcquisition){
					this.showTime.visible = false;
					return 1;
				}else{
					this.textTime = SetPhysicaTimer;
				}
			}
			this.showTime.visible = true;
			let hour = "0" + Math.floor(this.textTime / 3600);
			let minute = Math.floor((this.textTime % 3600)/60) + "";
			if (parseInt(minute) < 10) {
				minute = "0" + minute ;
			}
			let second = Math.floor(this.textTime % 60) + "";
			if (parseInt(second) < 10){
				second = "0" + second;
			}
			this.showTime.text = hour + ":" + minute + ":" + second;

			return 2;
		}
		getPrizeMultiple():number{
			return this.prizeMultiple;
		}

		addPhysicalValueRsq(physical:number){
			this.physical = physical;
			this.upPhysical()
		}
	}
}